<%@page import="java.util.*"%>
<%@page import="java.text.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%
    String path = request.getContextPath(); 
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+path+ "/"; 
     
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<title>代理商列表</title>
	<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" />
	<link rel="stylesheet" href="css/bootstrap-datetimepicker.css" />
    <link rel="stylesheet" href="bootstrap-table/dist/bootstrap-table.css">
    
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="css/font.css">
    
    <link rel="stylesheet" href="./css/x-admin.css" media="all">
</head>
<body>
<div class="x-nav">
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <xblock>
         <c:if test="${r.gRoleName!='代理人员'}">
             <button class="layui-btn" onclick="x_admin_show('新增代理商管理','jsp/sysUserManager/addOrUpdateAgent.jsp','600','')"><i class="layui-icon"></i>新增</button>
             <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
         </c:if>
      </xblock>  
      </div>
	  <div class="container" style="width: 100%">
	        <table id="demo-table" class="table table-bordered table-striped" style="width:100%">  
	        </table>
	  </div>
</body>
	<!-- 加载需要的js -->
	<script src="js/jquery-3.2.1.min.js"></script>
	<script src="js/common2.js"></script>
	<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
	<script src="js/jquery-bootstrap-pagination.js"></script>
    <script type="text/javascript" src="lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/xadmin.js"></script>
	<script src="js/qunit-1.11.0.js"></script>
	<script src="js/bootstrap-datetimepicker.js"></script>
	<script src="js/bootstrap-datetimepicker.zh-CN.js"></script>
	<script src="js/jquery.md5.js"></script>
	<script src="bootstrap-table/dist/bootstrap-table.js"></script>
	<script src="js/jquery.qrcode.js"></script>
	<script src="bootstrap-table/dist/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="js/xcConfirm.js" type="text/javascript" charset="utf-8"></script>
	
	<script src="lib/layui/layui.js" charset="utf-8"></script>
        <script src="js/x-layui.js" charset="utf-8"></script>
	<script type="text/javascript" charset="utf-8">

	$(function () {
	    initTable();	
	    
	   
	});
	
	 var  createEwm = function(e,uid){
		  $(e).qrcode({  
			    render : "canvas",    //设置渲染方式，有table和canvas，使用canvas方式渲染性能相对来说比较好  
			    text : "http://www.shituzx.com/shareRegister?uId="+uid,    //扫描二维码后显示的内容,可以直接填一个网址，扫描二维码后自动跳向该链接  
			    width : "150",               //二维码的宽度  
			    height : "150",              //二维码的高度  
			    background : "#ffffff",       //二维码的后景色  
			    foreground : "#000000",        //二维码的前景色  
			}); 
	}
	function eachEwm(){
		setTimeout(function(){
			$('.qrcode').each(function(e){
				var uid = $(this).attr('data-id');
				createEwm($(this),uid)
			})
		},500)
	}
    

	function doQuery(params){
	    $('#demo-table').bootstrapTable('refresh');    //刷新表格
	}
	
	

	function initTable(){
	    var url = "sysUser/getAgentInfo";
	    $('#demo-table').bootstrapTable({
	        method:'GET',
	        dataType:'json',
	        contentType: "application/x-www-form-urlencoded",
	        cache: false,
	        striped: true,                              //是否显示行间隔色
	        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
	        url:url,
	        height: $(window).height() - 110,
	        width:$(window).width(),
	        showColumns:true,
	        pagination:true,
	        queryParams : queryParams,
	        minimumCountColumns:2, 
	        pageNumber:1,                       //初始化加载第一页，默认第一页
	        pageSize:15,                       //每页的记录行数（*）
	        pageList: [15, 25, 50, 100],        //可供选择的每页的行数（*）
	        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
	        exportDataType: 'all',
	        responseHandler: responseHandler,
	        columns: [
		        { field: '', title: '序列', checkbox:true },
		        {
		            field : 'gAccount',
		            title : '代理商账号',
		            align : 'center',
		            valign : 'middle'
		           
		        },
		        {
		            field : 'gRemake',
		            title : '备注',
		            align : 'center',
		            valign : 'middle'
		           
		        },
		        {
		            field : 'gGrade',
		            title : '代理级别',
		            align : 'center',
		            valign : 'middle'
		           
		        },
		        {
		            field : 'gProportion',
		            title : '分成比例',
		            align : 'center',
		            valign : 'middle'
		           
		        },
		        {
		            field : 'gId',
		            title : '推广二维码',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		            	
		            	return '<div class="QR"><div class="qrcode" data-id='+row.gId+'></div></div>';
		            }
		        },
		        {
		            field : 'gId',
		            title : '推广链接',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		                 return "http://www.shituzx.com/shareRegister?uId="+value;
		             }
		        },
		        {
		            field : 'totalMoney',
		            title : '关联用户消费总额',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		                  if(value!=0){
		                	  return "￥"+value;
		                  }
		                  return value;
		             }
		        },
		        {
		            field : 'totalAgentMoney',
		            title : '代理消费总额(积分+课程)',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		                  if(value!=0){
		                	  return "￥"+value;
		                  }
		                  return value;
		             }
		        },
		        {
		            field : 'courseAgentMoney',
		            title : '购买代理课程总额',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		                  if(value!=0){
		                	  return "￥"+value;
		                  }
		                  return value;
		             }
		        },
		        {
		            field : 'integralMoney',
		            title : '购买积分总额',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		                  if(value!=0){
		                	  return "￥"+value;
		                  }
		                  return value;
		             }
		        },
		        {
		            field : 'shareMoney',
		            title : '提成总额',
		            align : 'center',
		            valign : 'middle',
		            formatter: function (value, row, index) {
		                  if(value!=0){
		                	  return "￥"+value;
		                  }
		                  return value;
		             }
		        },
		        {
		            field : 'upId',
		            title : '操作',
		            align : 'center',
		            valign : 'middle',
		            formatter: operateFormatter //自定义方法，添加操作按钮
		        }
		        
		        ],
		        onLoadSuccess:function(){
		        	eachEwm();
		        }
		    });
	    
	    	/* new QRCode($('.qrcode')[0], {
	    	  text: 'your content',
	    	  width: 256,
	    	  height: 256,
	    	  colorDark : '#000000',
	    	  colorLight : '#ffffff',
	    	  correctLevel : QRCode.CorrectLevel.H
	    	}); */
	    
	    	
	}
	
	
	
	
	
	function operateFormatter(value, row, index) {//赋予的参数
		console.log(row);
	    if(row.gStatus==1){
	    	return [
				'<c:if test="${r.gRoleName!='代理人员'}"><button  class="layui-btn "    onclick="admin_look(\''+ row.gId + '\')"  ><i class="layui-icon">&#xe604;</i> 编辑</button></c:if>',
				'<c:if test="${r.gRoleName!='代理人员'}"><button class="layui-btn "    onclick="updateStatus(\''+ row.gId + '\',2)"  ><i class="layui-icon">&#xe604;</i> 禁用</button></c:if>',
				'<button class="layui-btn "    onclick="look(\''+ row.gId + '\')"  ><i class="layui-icon">&#xe604;</i> 查看</button>'
				].join('');
	    }else{
	    	return [
				'<c:if test="${r.gRoleName!='代理人员'}"><button  class="layui-btn "    onclick="admin_look(\''+ row.gId + '\')"  ><i class="layui-icon">&#xe604;</i> 编辑</button></c:if>',
				'<c:if test="${r.gRoleName!='代理人员'}"><button class="layui-btn "    onclick="updateStatus(\''+ row.gId + '\',1)"  ><i class="layui-icon">&#xe604;</i> 恢复</button></c:if>',
				'<button class="layui-btn "    onclick="look(\''+ row.gId + '\')"  ><i class="layui-icon">&#xe604;</i> 查看</button>'
				].join('');
	    }
	}
	
	 //批量删除
 	function delAll() {
 	    //获取所有被选中的记录
 	    var rows = $("#demo-table").bootstrapTable('getSelections');
 	    if (rows.length== 0) {
 	    	layer.msg('请至少选择一个被删除项!',{icon:2,time:2000});	
 	        return;
 	    }
 	    var ids = '';
 	    for (var i = 0; i < rows.length; i++) {
 	        ids += rows[i]['gId'] + ",";
 	    }
 	    ids = ids.substring(0, ids.length - 1);
 	    deleteUser(ids);
 	}
	 
 	//删除
 	function deleteUser(ids) {
 		layer.confirm('确认要删除吗？',function(index){
             //发异步删除数据
 			$.ajax({
 	            url: "sysUser/delete",
 	            type: "post",
 	            data: {
 	            	gId: ids
 	            },
 	            success: function (data) {
 	            	if(data.code==200){
 				 		layer.msg('已删除!',{icon:3,time:3000});				 		
 				 		doQuery();
			 		}else{
			 			layer.msg('删除失败!',{icon:3,time:3000});			
			 		}
 				} 
 	        });
         });       
 	 }
    
     /*编辑*/
     function admin_look(gId){
     	 x_admin_show("查看详情","sysUser/userDetail?gId="+gId+"&type=2","800"); 
     	 /*   window.location.href="course/couserDetails?classId="+classId+"&courseId="+courseId+"&id="+id;   */ 
     }
     
     /*编辑*/
     function look(gId){
     	 x_admin_show("查看关联用户列表","jsp/sysUserManager/juniorInfoList.jsp?gId="+gId,"1200"); 
     	 /*   window.location.href="course/couserDetails?classId="+classId+"&courseId="+courseId+"&id="+id;   */ 
     }
 
     //禁用启用
     function updateStatus(id,type) {
 		layer.confirm('确认要执行该操作吗？',function(index){
             //发异步删除数据
 			$.ajax({
 			    type:"get",
 				url:"sysUser/updateStatus",
 				data:{gId:id,status:type
 					},
 				success:function(data){
 					 //console.log(data)
 				 	if(data.code==200){
 				 		layer.msg('操作成功!',{icon:3,time:3000});	
 				 		doQuery();
 				        //window.location.href="jsp/advertisingManager/parentAdvertisingList.jsp";
 				 		}else {
 				 			layer.msg('操作失败!',{icon:3,time:3000});	
 				 		}
 				 	} 
 	  		 });
         });
 	}
	   
	function operateFormatterImg(value, row, index){			
		 
	} 

		 
	function responseHandler(res) { 
		console.log(res);
	    if (res) {
	        return {
	            "rows" : res.data,
	            "total" : res.totalRecord
	        };
	    } else {
	        return {
	            "rows" : [],
	            "total" : 0
	        };
	    }
	}
	
	function queryParams(params) {
		var param = {    		
	        limit : this.limit, // 页面大小
	        offset : this.offset, // 页码
	    
	        pageNumber : this.pageNumber,
	        pageSize : this.pageSize
	    }
	  

	    return param;
	} 
	//初始化弹出框
	$(function () {
	  	$('[data-toggle="popover"]').popover()
	});
	
	 //时间格式化
   	function timeStamp2String(time){
   	    var datetime = new Date();
   	    datetime.setTime(time);
   	    var year = datetime.getFullYear();
   	    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
   	    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
   	    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
   	    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
   	    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
   	    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second
   	}
    </script>
</body>
</html>